import { createRouter, createWebHashHistory } from 'vue-router'
import routes from './routes'
import store from '@/store'
import { ElMessage } from 'element-plus'

// 路由实例
const router = createRouter({
  routes,
  history: createWebHashHistory(),
})

 
// 路由守卫
// 登录鉴权
router.beforeEach((to, from, next) => {
  if (to.name !== 'login' && !store.getters['authority/isLogin']) {
    ElMessage({ type: 'error', message: '请登录后访问' })
    next({ name: 'login' })
  }
  else next()
})
// 权限判断
router.beforeEach((to, from, next) => {
  const { adminname, checkedkeys } = store.state.authority
  if (to.name !== 'login' && adminname !== 'admin' && !checkedkeys.includes(to.matched[1].name)) {
    ElMessage({ type: 'error', message: '无权限访问, 请重新登录' })
    next({ name: 'login' })
  }
  else next()
})

router.afterEach((to, from) => {
  // ...
})

export default router
